package com.esiag.isidis.fi.pdsteam.service;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Observable;

import com.esiag.isidis.fi.pdsteam.entity.Machine;

public class ServerReceiver extends Observable implements IReceiver {

	public void startReceive(Machine machine) {
		try {

			DatagramSocket socket = new DatagramSocket(machine.getPort());
			byte[] receiveData = new byte[1024];
			while (true) {
				InetAddress inetAddress = InetAddress
						.getByName(machine.getIp());
				DatagramPacket receivePacket = new DatagramPacket(receiveData,
						receiveData.length);
				socket.receive(receivePacket);
				for (int i = 0; i < receiveData.length; i++) {
					if(receiveData[i] != 0) receiveData[i] = (byte) (receiveData[i]-48);
				}
				setChanged();
				notifyObservers(receivePacket.getData());

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
